//
//  Hot.m
//  HYHOME
//
//  Created by cjy on 16/2/28.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "Hot.h"
#import "DKNightVersion.h"
#import "NetworkingHandle.h"
#import "HotCell.h"
#import "PushHot.h"
#import "HotModel.h"
#import "MJRefresh.h"
#import "BuyViewController.h"
#import "DKNightVersion.h"
@interface Hot ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *array;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, retain) NSString *strpage;
@property (nonatomic,retain)NSMutableArray *arrdetail;
@end

@implementation Hot


- (void)dealloc{
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [super dealloc];



}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem =[[ UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
    self.title = @"Interesting";
    

   
    [self createtableView];
    [self handle];

}

- (void)handle{

    self.array = [NSMutableArray array];
    self.arrdetail = [NSMutableArray array];
    self.strpage = @"http://106.187.100.229:8090/api/design?page=1&order=&category=&";
    
    self.page = 1;
   
//   http://106.187.100.229:8090/api/design?page=1&order=&category=&56b5e947d1685ee92475ade7
    [NetworkingHandle networkingWithURL:self.strpage completeHandel:^(NSURLResponse *response, NSData *data, id result) {
        
        
        for (NSDictionary *dic in result) {
            
            
            HotModel *model = [[HotModel alloc]init];
            

            [model setValuesForKeysWithDictionary:dic];

            [self.array addObject:model];
            
            
            [model release];
            
        }
        [self.tableView reloadData];
        
        
        
    }];
    
    


}

- (void)handle2{

//    self.array = [NSMutableArray array];
    [NetworkingHandle networkingWithURL:self.strpage completeHandel:^(NSURLResponse *response, NSData *data, id result) {
        
        
        for (NSDictionary *dic in result) {
            
            
            HotModel *model = [[HotModel alloc]init];
            
            //            model.description1 = [dic objectForKey:@"description"];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.array addObject:model];
            
            [model release];
        }
        [self.tableView reloadData];
        
        
        
    }];





}

- (void)createtableView{
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];

    [self.tableView release];
    

    [self.tableView registerClass:[HotCell class] forCellReuseIdentifier:@"pool"];
    
 

    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            
            self.strpage = [NSString stringWithFormat:@"http://106.187.100.229:8090/api/design?page=%ld&order=&category=&",++self.page];
            
            [self handle2];
            NSLog(@"str: %@", self.strpage);
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            
        });
        
        
    }];
    
    
    [self.tableView.mj_header beginRefreshing];
    

    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    

    self.tableView.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);
    self.tableView.dk_separatorColorPicker = DKColorWithRGB(0xaaaaaa, 0x313131);


}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    
    return self.array.count;


}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HotCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    

    HotModel *model = [self.array objectAtIndex:indexPath.row];
   
    [cell passModel:model];
    
    
    cell.backgroundColor = [UIColor colorWithRed:245 / 256.f green:245 /256.f blue:255/256.f alpha:1];

    cell.dk_backgroundColorPicker  = DKColorWithRGB(0xffffff, 0x343434);

    cell.selectionStyle = UITableViewCellAccessoryNone;
    return cell;


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 260;


}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    PushHot *hot = [[PushHot alloc]init];
    
    
    
    hot.arrrr = [self.array[indexPath.row] detail];

    HotModel *hotmodel = self.array[indexPath.row];
    hot.des = [hotmodel description1];
    hot.title2 = [hotmodel title];
    
    hot.buylink = [hotmodel buyLink];
    
    NSLog(@"%@",hot.buylink);

    [self.navigationController pushViewController:hot animated:YES];
    [hot release];



}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
